/**
 * 单独封装一个时间相关的类，为了解决后期日期处理问题
 */
export class LocalDate extends Date {
    static now() {
        return Date.now();
    }

    static parse(s: string) {
        return Date.parse(s);
    }

    static formateString(t?: number) {
        let t1 = t ? new LocalDate(t) : new LocalDate();
        return t1.formateString();
    }

    toLen(num: number, len: number) {
        let out = num.toString();
        while (out.length < len) out = '0' + out;
        return out;
    }

    formateString() {
        return `${this.toLen(this.getFullYear(), 4)}-${this.toLen(this.getMonth() + 1, 2)}-${this.toLen(this.getDate(), 2)} ${this.toLen(this.getHours(), 2)}:${this.toLen(this.getMinutes(), 2)}:${this.toLen(this.getSeconds(), 2)}`;
    }

    static isMonthly(timeA: number, timeB: number) {
        let tA = new LocalDate(timeA);
        let tB = new LocalDate(timeB);

        if (tA.getUTCFullYear() == tB.getUTCFullYear() &&
            tA.getUTCMonth() == tB.getUTCMonth()) {
            return true;
        }

        return false;
    }

    static isDaily(timeA: number, timeB: number) {
        let tA = new LocalDate(timeA);
        let tB = new LocalDate(timeB);

        if (tA.getUTCFullYear() == tB.getUTCFullYear() &&
            tA.getUTCMonth() == tB.getUTCMonth() &&
            tA.getUTCDate() == tB.getUTCDate()) {
            return true;
        }

        return false;
    }

    static isTime(timeA: number, timeB: number, type: "year" | "month" | "date" | "hour" | "minute" | "second") {
        let tA = new LocalDate(timeA);
        let tB = new LocalDate(timeB);

        switch (type) {
            default:
            case 'second':
                if (tA.getUTCSeconds() != tB.getUTCSeconds()) return false;
            case 'minute':
                if (tA.getUTCMinutes() != tB.getUTCMinutes()) return false;
            case 'hour':
                if (tA.getUTCHours() != tB.getUTCHours()) return false;
            case "date":
                if (tA.getUTCDate() != tB.getUTCDate()) return false;
            case "month":
                if (tA.getUTCMonth() != tB.getUTCMonth()) return false;
            case "year":
                if (tA.getUTCFullYear() != tB.getUTCFullYear()) return false;
                break;
        }

        return true;
    }


    static isYearly(timeA: number, timeB: number) {
        let tA = new LocalDate(timeA);
        let tB = new LocalDate(timeB);

        if (tA.getUTCFullYear() == tB.getUTCFullYear()) {
            return true;
        }

        return false;
    }

    static isWeekly(timeA: number, timeB: number) {
        let tA = new LocalDate(timeA);
        tA.setUTCHours(0, 0, 0, 0);
        let tB = new LocalDate(timeB);
        tB.setUTCHours(0, 0, 0, 0);
        let d = tA.getUTCDay() + Math.floor((tA.getTime() - tB.getTime()) / (1000 * 60 * 60 * 24));
        if (d >= 0 && d < 7) {
            return true;
        }

        return false;
    }
}